/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  TietoEnator
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "../roommodel.h"
#include "util/conversion.h"
#include "core/iroom.h"
#include "core/staticroomitem.h"
#include "core/moveableroomitem.h"

using namespace greenhills::client::mva;
using namespace greenhills::core;
using namespace greenhills::util;

CRoomModel::CRoomModel(IRoom *pRoom) : QAbstractTableModel()
{
    mRoom = pRoom;
    mRoom->registerListener(this);
}

CRoomModel::~CRoomModel()
{
    mRoom->unregisterListener(this);
}

int CRoomModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return mRoom->count();
}

int CRoomModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    if (parent.isValid())
        return 0;

    return ECOLUMNS_COUNT;
}

QModelIndex CRoomModel::index(int row, int column, const QModelIndex
        &parent) const
{
    if (!hasIndex(row, column, parent))
        return QModelIndex();

    if (parent.isValid())
        return QModelIndex();
    else
        return createIndex(row, column, mRoom->itemAt(row));
}

QVariant CRoomModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    IRoomItem *roomItem = static_cast<IRoomItem*>(index.internalPointer());
    Q_ASSERT(roomItem);

    CStaticRoomItem *positionedRoomItem =
            dynamic_cast<CStaticRoomItem *>(roomItem);
    CMoveableRoomItem *moveableRoomItem =
            dynamic_cast<CMoveableRoomItem *>(roomItem);
    
    switch(role)
    {
    case Qt::DisplayRole:
        {
            switch (index.column())
            {
            case ColumnId:
                return QString::number(roomItem->id());
            case ColumnName:
                return roomItem->name().c_str();
            case ColumnType:
                return roomItem->type().toString().c_str();
            case ColumnLatitude:
                return positionedRoomItem ?
                        positionedRoomItem->position().latitude().toString() :
                        QString("-");
            case ColumnLongitude:
                return positionedRoomItem ?
                        positionedRoomItem->position().longitude().toString() :
                        QString("-");
            case ColumnAltitude:
                return positionedRoomItem ?
                        QString::number(positionedRoomItem->altitude()) :
                        QString("-");
            case ColumnTimeStamp:
                return positionedRoomItem ?
                        QString(sec2str(positionedRoomItem->timeStamp()).c_str()):
                        QString("-");
            case ColumnMovementSpeed:
                return moveableRoomItem ?
                        QString::number(moveableRoomItem->movementSpeed()) :
                        QString("-");
            case ColumnMovementDirection:
                return moveableRoomItem ?
                        QString::number(moveableRoomItem->movementDirection()) :
                        QString("-");
            }
            break;
        }// end of Qt::DisplayRole switch
    }// end of role switch

    return QVariant();
}

QVariant CRoomModel::headerData(int section, Qt::Orientation orientation,
        int role) const
{
    if (role == Qt::DisplayRole)
    {
        if (orientation == Qt::Horizontal)
        {
            switch(section)
            {
            case ColumnId:
                return QString("Id");
            case ColumnName:
                return  QString("Name");
            case ColumnType:
                return  QString("Type");
            case ColumnLatitude:
                return  QString("Latitude");
            case ColumnLongitude:
                return  QString("Longitude");
            case ColumnAltitude:
                return  QString("Altitude");
            case ColumnTimeStamp:
                return  QString("Time");
            case ColumnMovementSpeed:
                return  QString("Speed");
            case ColumnMovementDirection:
                return  QString("Direction");
            }
        }
        else
            return QString("Item %1").arg(section);
    }

    return QAbstractTableModel::headerData(section,orientation,role);
}

void CRoomModel::aboutToAddItem(IRoom *pRoom,
    const CItemId &pItemId)
{
    Q_UNUSED(pItemId);

    QAbstractTableModel::beginInsertRows(QModelIndex(),
        pRoom->count(), pRoom->count());
}

void CRoomModel::aboutToUpdateItem(IRoom *pRoom,
    const CItemId &pItemId)
{
    Q_UNUSED(pRoom);
    Q_UNUSED(pItemId);
}

void CRoomModel::aboutToRemoveItem(IRoom *pRoom,
    const CItemId &pItemId)
{
    int idx = pRoom->indexOf(pItemId);
    Q_ASSERT(idx >= 0);
    QAbstractTableModel::beginRemoveRows(QModelIndex(), idx, idx);
}

void CRoomModel::itemAdded(IRoom *pRoom,
    const CItemId &pItemId, const std::string &pItemName,
    const CItemType &pItemType)
{
    Q_UNUSED(pRoom);
    Q_UNUSED(pItemId);
    Q_UNUSED(pItemName);
    Q_UNUSED(pItemType);

    QAbstractTableModel::endInsertRows();
}

void CRoomModel::itemUpdated(IRoom *pRoom,
    const CItemId &pItemId, const unsigned char *pData,
    unsigned int pLength)
{
    Q_UNUSED(pData);
    Q_UNUSED(pLength);

    int idx = pRoom->indexOf(pItemId);
    Q_ASSERT(idx >= 0);
    Q_EMIT dataChanged(index(idx, 0), index(idx, CRoomModel::ECOLUMNS_COUNT-1));
}

void CRoomModel::itemRemoved(IRoom *pRoom, const CItemId &pItemId)
{
    Q_UNUSED(pRoom);
    Q_UNUSED(pItemId);

    QAbstractTableModel::endRemoveRows();
}
